<?php

declare(strict_types=1);

/**
 * Copyright (c) 2021-2025 guanguans<ityaozm@gmail.com>
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 *
 * @see https://github.com/guanguans/laravel-skeleton
 */

namespace App\Models\Concerns;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Request;

/**
 * @see https://github.com/Zakarialabib/myStockMaster/tree/master/app/Traits
 *
 * @mixin \Illuminate\Database\Eloquent\Model
 */
trait Trashed
{
    /**
     * Scope a query to get with transed or only transhed resource.
     *
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function scopeTrashed(Builder $query): Builder
    {
        if (!empty(Request::getFacadeRoot()->get('trashed')) && Request::getFacadeRoot()->get('trashed') === 'with') {
            return $query->withTrashed();
        }

        if (!empty(Request::getFacadeRoot()->get('trashed')) && Request::getFacadeRoot()->get('trashed') === 'only') {
            return $query->onlyTrashed();
        }

        return $query;
    }
}
